
import java.util.Date;
import java.util.List;
import java.util.TimerTask;
import java.util.concurrent.CompletionService;

public class AutoFlushTask extends TimerTask {

	Information info = null;
	
	CompletionService<ScreenShotVO> takeScreencompletionService = null;
	
	String filepath = null;
	
	String mirrorpath = null;
	
	public AutoFlushTask(Information info, 
			CompletionService<ScreenShotVO> takeScreencompletionService,
			String filepath,
			String mirrorpath) {
		this.info = info;
		this.takeScreencompletionService = takeScreencompletionService;
		this.filepath = filepath;
		this.mirrorpath = mirrorpath;
	}
	
	@Override
	public void run() {
		List<ShotVO> shotList = UrlToImage.getShotList();
		int count = 1;
		if( shotList != null ) {
			System.out.println("auto-flush start with "+shotList.size()+" job at "+new Date());
			for(int i=0; i<shotList.size(); i++) {
				TakeScreen t = new TakeScreen(shotList.get(i).getReportsId(), shotList.get(i).getPageUrl(), filepath, mirrorpath);
				takeScreencompletionService.submit(t);
				info.incrementReserved();
				System.out.println("submit at "+count+": "+shotList.get(i).getPageUrl());
				try { Thread.sleep(1000); } 
				catch (InterruptedException e) {
					Thread.currentThread().interrupt();
					return;
				}
				++count;
			}
		}
		System.out.println("auto-flush done at "+new Date());
	}

}
